package com.szj.pet.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szj.pet.hospital.common.result.Result;
import com.szj.pet.hospital.model.hosp.PetInformation;
import com.szj.pet.hospital.service.PetInformationService;
import com.szj.pet.hospital.vo.hosp.PetInformationQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@Api(tags = "宠物体检信息管理")
@RestController
@RequestMapping("admin/hosp/petInformation")
//@CrossOrigin
public class PetInformationController {
    @Autowired
    private PetInformationService petInformationService;

    //1 查询所有宠物体检信息
    @ApiOperation(value = "获取所有宠物体检信息")
    @GetMapping("findAll")
    public Result findAllPetInformation() {
        //调用service的方法
        List<PetInformation> list = petInformationService.list();
        return Result.ok(list);
    }

    //2 逻辑删除宠物体检信息
    @ApiOperation(value = "逻辑删除宠物体检信息")
    @DeleteMapping("{id}")
    public Result removePetInformation(@PathVariable Long id) {
        boolean flag = petInformationService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3 条件查询带分页
    @ApiOperation(value = "宠物体检信息条件查询带分页")
    @PostMapping("findPagePetInformation/{current}/{limit}")
    public Result findPagePetInformation(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) PetInformationQueryVo petInformationQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<PetInformation> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<PetInformation> wrapper = new QueryWrapper<>();
        String name = petInformationQueryVo.getName();//名字
        String doctorName = petInformationQueryVo.getDoctorName();//医生
        String userName = petInformationQueryVo.getUserName();//手机号码

        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", petInformationQueryVo.getName());
        }
        if (!StringUtils.isEmpty(doctorName)) {
            wrapper.eq("doctor_name", petInformationQueryVo.getDoctorName());
        }
        if (!StringUtils.isEmpty(userName)) {
            wrapper.like("user_name", petInformationQueryVo.getUserName());
        }
        wrapper.orderByDesc("update_time");

        //调用方法实现分页查询
        IPage<PetInformation> pagePetInformation = petInformationService.page(page, wrapper);

        //返回结果
        return Result.ok(pagePetInformation);
    }

    //4 添加宠物体检信息
    @PostMapping("savePetInformation")
    public Result savePetInformation(@RequestBody PetInformation petInformation) throws ParseException {

        //调用service
        boolean save = petInformationService.save(petInformation);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取宠物体检信息
    @GetMapping("getPetInformation/{id}")
    public Result getPetInformation(@PathVariable Long id) {

        PetInformation petInformation = petInformationService.getById(id);
        return Result.ok(petInformation);
    }
    //6 修改宠物体检信息
    @PostMapping("updatePetInformation")
    public Result updatePetInformation(@RequestBody PetInformation petInformation) {
        boolean flag = petInformationService.updateById(petInformation);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除宠物体检信息
    @DeleteMapping("batchRemove")
    public Result batchRemovePetInformation(@RequestBody List<Long> idList) {
        petInformationService.removeByIds(idList);
        return Result.ok();
    }
}
